package com.changpay.zywy.ui.activity.count;

import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.ruffian.library.widget.RTextView;
import com.changpay.zywy.R;
import com.changpay.zywy.ui.dialog.SelectMonthDialog;
import com.changpay.zywy.utils.DateUtil;
import com.changpay.zywy.utils.StringUtil;
import kale.ui.view.dialog.EasyDialog;
import lecho.lib.hellocharts.view.PieChartView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AndroidIntexh1 on 2018/12/20.
 */

public class CountTrandMonthTwoView extends RelativeLayout {

    RelativeLayout rvMore;
    TextView tvTitle;
    ImageView ivMore;
    LinearLayout llContent;


    RTextView tvTimeSearch;


    TextView tv_trand_total_text; // 交易总数


    TextView tv_trand_times_text; // 交易笔数


    RadarChart radarTrand;  //  本月交易总量
    RadarChart radarTimes;  // 本月交易总笔数

    CountChartActionClose countChartOtherClose;
    public boolean isDisplay = false;


    private View view;
    private DatePickerDialog.OnDateSetListener onDateSetListener;
    private String month;
    private TextView tv_top_card;
    private TextView tv_top_bank_card;
    private TextView tv_top_scan_pay;
    private TextView tv_top_fast_pay;
    private TextView tv_top_cloud_pay;
    private TextView tv_bottom_card_pay;
    private TextView tv_bottom_bank_card_pay;
    private TextView tv_bottom_fast_pay;
    private TextView tv_bottom_scan_pay;
    private TextView tv_bottom_cloud_pay;

    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    FragmentManager fragmentManager;

    public CountTrandMonthTwoView(Context context) {
        this(context, null);
    }

    public CountTrandMonthTwoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        month = DateUtil.INSTANCE.getYearMonth(System.currentTimeMillis());
        view = View.inflate(context, R.layout.count_item_total_tranding_two, this);
        tvTitle = view.findViewById(R.id.tv_title);
        tvTimeSearch = view.findViewById(R.id.tv_time_search);
        tv_trand_total_text = view.findViewById(R.id.tv_trand_total_text);
        tv_trand_times_text = view.findViewById(R.id.tv_trand_times_text);
        radarTrand = view.findViewById(R.id.trend_radarChart);
        radarTimes = view.findViewById(R.id.trend_radarChart_two);

        tv_top_card = view.findViewById(R.id.tv_top_card);
        tv_top_bank_card = view.findViewById(R.id.tv_top_bank_card);
        tv_top_scan_pay = view.findViewById(R.id.tv_top_scan_pay);
        tv_top_fast_pay = view.findViewById(R.id.tv_top_fast_pay);
        tv_top_cloud_pay = view.findViewById(R.id.tv_top_cloud_pay);


        tv_bottom_card_pay = view.findViewById(R.id.tv_bottom_card_pay);
        tv_bottom_bank_card_pay = view.findViewById(R.id.tv_bottom_bank_card_pay);
        tv_bottom_fast_pay = view.findViewById(R.id.tv_bottom_fast_pay);
        tv_bottom_scan_pay = view.findViewById(R.id.tv_bottom_scan_pay);
        tv_bottom_cloud_pay = view.findViewById(R.id.tv_bottom_cloud_pay);

        tvTimeSearch.setOnClickListener(view -> {
//                DialogUtils.showDatePickerDialog(context, onDateSetListener, Calendar.getInstance());
            SelectMonthDialog.Builder builder = new SelectMonthDialog.Builder(context);
            builder.setOnDialogClickListener((v, month) -> {
                tv_trand_total_text.setText(month + "总交易量");
                tv_trand_times_text.setText(month + "总交易笔数");
                initDateData(month);
            });
            EasyDialog build = builder.build();
            build.setCancelable(true);
            build.show(fragmentManager);
        });

        initRadar();
    }

    private void initRadar() {

        radarTrand.getDescription().setEnabled(false);
        radarTrand.setTouchEnabled(false);
        radarTrand.getLegend().setEnabled(false);
        radarTrand.setWebLineWidth(1);
        radarTrand.setWebLineWidthInner(1);
        radarTrand.setWebAlpha(100);
        radarTrand.setWebColorInner(Color.parseColor("#FF6C5D6E")); // 网圈圈颜色
        radarTrand.setWebColor(Color.parseColor("#FF6C5D6E")); // 网圈圈颜色
        XAxis xAxis = radarTrand.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLabels(true);
        xAxis.setLabelCount(5,true);
        xAxis.setTextSize(12f);
        xAxis.setTextColor(Color.parseColor("#FF5B5B5B"));
        xAxis.setCenterAxisLabels(true);//设置标签居中

        YAxis yAxis = radarTrand.getYAxis();
        yAxis.setDrawGridLines(false);
        yAxis.setLabelCount(5, true);
        yAxis.setTextColor(Color.parseColor("#FF5B5B5B"));
        yAxis.setAxisMinimum(0f);


        radarTimes.getDescription().setEnabled(false);
        radarTimes.setTouchEnabled(false);
        radarTimes.getLegend().setEnabled(false);
        radarTimes.setWebLineWidth(1);
        radarTimes.setWebLineWidthInner(1);
        radarTimes.setWebAlpha(100);
        radarTimes.setWebColorInner(Color.parseColor("#FF6C5D6E")); // 网圈圈颜色
        radarTimes.setWebColor(Color.parseColor("#FF6C5D6E")); // 网圈圈颜色
        XAxis xAxisTwo = radarTimes.getXAxis();
        xAxisTwo.setDrawGridLines(false);
        xAxisTwo.setDrawLabels(true);
        xAxisTwo.setTextSize(12f);
        xAxisTwo.setTextColor(Color.parseColor("#FF5B5B5B"));
        xAxisTwo.setCenterAxisLabels(true);//设置标签居中

        YAxis yAxisTwo = radarTimes.getYAxis();
        yAxisTwo.setDrawGridLines(false);
        yAxisTwo.setLabelCount(5, true);
        yAxisTwo.setTextColor(Color.parseColor("#FF5B5B5B"));
        yAxisTwo.setAxisMinimum(0f);
    }

    private void initDateData(String month) {
        countChartOtherClose.loadTrandMonthCharData(month);
    }

    public void setData(double cloudPay, double cardPay, double bankCardPay, double fastPay, double scanPay,
                        int cloudPayTimes, int cardPayTimes, int bankCardPayTimes, int fastPayTimes, int scanPayTimes) {

        formatFloat(tv_top_cloud_pay,cloudPay,"元");
        formatFloat(tv_top_card,cardPay,"元");
        formatFloat(tv_top_bank_card,bankCardPay,"元");
        formatFloat(tv_top_fast_pay,fastPay,"元");
        formatFloat(tv_top_scan_pay,scanPay,"元");


        formatInt(tv_bottom_cloud_pay,cloudPayTimes,"笔");
        formatInt(tv_bottom_card_pay,cardPayTimes,"笔");
        formatInt(tv_bottom_bank_card_pay,bankCardPayTimes,"笔");
        formatInt(tv_bottom_fast_pay,fastPayTimes,"笔");
        formatInt(tv_bottom_scan_pay,scanPayTimes,"笔");


        // 构建x轴
        radarTrand.clear();
        List<String> Xs = new ArrayList<>();
        Xs.add("信用卡");
        Xs.add("银行卡");
        Xs.add("扫码支付");
        Xs.add("快捷支付");
        Xs.add("云闪付");
        XAxis xAxis = radarTrand.getXAxis();
        xAxis.setLabelCount(Xs.size());
        xAxis.setValueFormatter(new IndexAxisValueFormatter(Xs));


        List<RadarEntry> radarEntries = new ArrayList<RadarEntry>();

        radarEntries.add(new RadarEntry((float) cardPay));
        radarEntries.add(new RadarEntry((float) bankCardPay));
        radarEntries.add(new RadarEntry((float) scanPay));
        radarEntries.add(new RadarEntry((float) fastPay));
        radarEntries.add(new RadarEntry((float) cloudPay));


        RadarDataSet radarDataSet = new RadarDataSet(radarEntries, "数据1");
        radarDataSet.setFillColor(Color.parseColor("#FABF13"));  // 浅绿色  商户交易量
        radarDataSet.setDrawFilled(true);
        radarDataSet.setLineWidth(0f);

        List<IRadarDataSet> sets = new  ArrayList<IRadarDataSet>();
        sets.add(radarDataSet);

        RadarData radarData = new RadarData(sets);
        radarData.setDrawValues(false);
        radarTrand.setData(radarData);



        // 构建x轴 .分别获取前6天
        radarTimes.clear();
        List<String> XsTwo = new ArrayList<>();
        XsTwo.add("信用卡");
        XsTwo.add("银行卡");
        XsTwo.add("扫码支付");
        XsTwo.add("快捷支付");
        XsTwo.add("云闪付");
        XAxis xAxisTwo = radarTimes.getXAxis();
        xAxisTwo.setLabelCount(Xs.size());
        xAxisTwo.setValueFormatter(new IndexAxisValueFormatter(XsTwo));


        List<RadarEntry> radarEntriesTwo = new ArrayList<RadarEntry>();
        radarEntriesTwo.add(new RadarEntry((float) cardPayTimes));
        radarEntriesTwo.add(new RadarEntry((float) bankCardPayTimes));
        radarEntriesTwo.add(new RadarEntry((float) scanPayTimes));
        radarEntriesTwo.add(new RadarEntry((float) fastPayTimes));
        radarEntriesTwo.add(new RadarEntry((float) cloudPayTimes));


        RadarDataSet radarDataSetTwo = new RadarDataSet(radarEntriesTwo, "数据2");
        radarDataSetTwo.setFillColor(Color.parseColor("#FABF13"));  // 浅绿色  商户交易量
        radarDataSetTwo.setDrawFilled(true);
        radarDataSetTwo.setLineWidth(0f);

        List<IRadarDataSet> setsTwo = new  ArrayList<IRadarDataSet>();
        setsTwo.add(radarDataSetTwo);

        RadarData radarDataTwo = new RadarData(setsTwo);
        radarDataTwo.setDrawValues(false);
        radarTimes.setData(radarDataTwo);

//
//
//
//        formatInt(tv_total_cloud_times,cloudPayTimes,"笔");
//        formatInt(tv_total_card_times,cardPayTimes,"笔");
//        formatInt(tv_total_bank_card_times,bankCardPayTimes,"笔");
//        formatInt(tv_total_fast_times,fastPayTimes,"笔");
//        formatInt(tv_total_scan_times,scanPayTimes,"笔");

//        initFloatPerChart(ct_trand_total, cloudPay,  cardPay,  bankCardPay,  fastPay,  scanPay);
//        initPerChart(ct_trand_times,   cloudPayTimes,  cardPayTimes,  bankCardPayTimes,  fastPayTimes,  scanPayTimes);
    }

    private void initPerChart(PieChartView chart, int h9AddAction, int mp70AddAction, int zhibeijiAction, int h9Action, int mp70Action) {

    }

    private void initFloatPerChart(PieChartView chart, double h9AddAction, double mp70AddAction, double zhibeijiAction, double h9Action, double mp70Action) {

    }


    public void setListener(CountChartActionClose listener) {
        countChartOtherClose = listener;
    }

    public void setTitle(String title) {
        if (tvTitle != null) tvTitle.setText(title);
    }

    public interface CountChartActionClose {

        void loadTrandMonthCharData(String month);
    }

    private void formatFloat(TextView view,double value, String danwei) {
        if (value >= 10000) {
            if (value >= 100000000) {
                double n = value / 100000000;

                view.setText(StringUtil.INSTANCE.calculateProfit(n) + "亿" + danwei);
            } else {
                double n = value / 10000;

                view.setText(StringUtil.INSTANCE.calculateProfit(n) + "万" + danwei);
            }
        } else {
            view.setText(StringUtil.INSTANCE.calculateProfit(value)  + danwei);
        }
    }

    private void formatInt(TextView view,int value, String danwei) {
        if (value >= 10000) {
            if (value >= 100000000) {
                double n = value / 100000000;
                view.setText(StringUtil.INSTANCE.calculateProfit(n) + "万" + danwei);
            } else {
                double n = value / 10000;
                view.setText(StringUtil.INSTANCE.calculateProfit(n) + "亿" + danwei);
            }
        } else {
            view.setText(value + danwei);
        }
    }

}
